package iso
{
	import flash.geom.Point;

	public class IsoGrid extends IsoDisplayObject
	{
		private var _gridType:int=0;
		private var _cellSize:Number;
		private var _rowSize:Number;
		private var _colsSize:Number;
		private var _color:uint;
		private var _gridAlpha:Number;
		
		public function IsoGrid(gridType:int, cellSize:Number, rowSize:int, colsSize:int, color:uint=0x666666, gridAlpha:Number=0.5)
		{
			_gridType=gridType;
			_cellSize=cellSize;
			_rowSize=rowSize;
			_colsSize=colsSize;
			_color = color;
			_gridAlpha=gridAlpha;
		}
		
		/**
		 *画网格
		 */
		private function drawGrid1():void 
		{
			graphics.clear();
			graphics.lineStyle(0, _color, _gridAlpha);
			
			var pt:Point = new Point();
			
			var i:int = 0;
			while (i <= _colsSize)
			{
				pt = IsoUtils.spaceToScreen(new IsoPoint(_cellSize * i));
				graphics.moveTo(pt.x, pt.y);
				
				pt = IsoUtils.spaceToScreen(new IsoPoint(_cellSize * i, 0, _cellSize * _rowSize));
				graphics.lineTo(pt.x, pt.y);
				
				i++;
			}
			
			i = 0;
			while (i <= _rowSize)
			{
				pt = IsoUtils.spaceToScreen(new IsoPoint(0, 0, _cellSize * i));
				graphics.moveTo(pt.x, pt.y);
				
				pt = IsoUtils.spaceToScreen(new IsoPoint(_cellSize * _colsSize, 0, _cellSize * i));
				graphics.lineTo(pt.x, pt.y);
				
				i++;
			}
			
			pt = IsoUtils.spaceToScreen(new IsoPoint(0, 0));
			graphics.moveTo(pt.x, pt.y);
			graphics.lineStyle(0, 0, 0);
			graphics.beginFill(0xFF0000, 0.0);
			
			pt = IsoUtils.spaceToScreen(new IsoPoint(_cellSize * _colsSize, 0));
			graphics.lineTo(pt.x, pt.y);
			
			pt = IsoUtils.spaceToScreen(new IsoPoint(_cellSize * _colsSize, 0, _cellSize * _rowSize));
			graphics.lineTo(pt.x, pt.y);
			
			pt = IsoUtils.spaceToScreen(new IsoPoint(0, 0, _cellSize * _rowSize));
			graphics.lineTo(pt.x, pt.y);
			
			pt = IsoUtils.spaceToScreen(new IsoPoint(0, 0));
			graphics.lineTo(pt.x, pt.y);
			graphics.endFill();
			
		}
		
		private var _mapWidth:int;		//地图网格宽度
		private var _mapHeight:int;		//地图网格高度
		private var _tilePixelWidth:int;	//一个网格的象素宽
		private var _tilePixelHeight:int;	//一个网格的象素高
		private var _gridLineColor:uint = 0xbbbbbb;//线条颜色
		
		private var _wHalfTile:int;	//网格象素宽的一半
		private var _hHalfTile:int;	//网格象素高的一半
		//画制网格
		public function drawGrid2():void
		{
			this._mapWidth = colsSize*cellSize;
			this._mapHeight = rowSize*cellSize;
			this._tilePixelWidth = cellSize;
			this._tilePixelHeight = cellSize;
			
			var col:int =  Math.floor(this._mapWidth / this._tilePixelWidth);
			var row:int =  Math.round(this._mapHeight / this._tilePixelHeight);
			
			this._wHalfTile = int(this._tilePixelWidth);
			this._hHalfTile = int(this._tilePixelHeight/2);
			
			graphics.clear();
			graphics.lineStyle(1, _gridLineColor, 1);
			
			var dblMapWidth:int = col*2 + 1;
			var dblMapHeight:int = row*2 + 1;
			for (var i:int=1; i<dblMapWidth; i = i+2)
			{
				
				graphics.moveTo( i*this._wHalfTile-cellSize, 0 );
				if (dblMapHeight+i >= dblMapWidth)
				{
					graphics.lineTo( dblMapWidth*this._wHalfTile-cellSize, (dblMapWidth-i)*this._hHalfTile );
				}
				else
				{
					graphics.lineTo( (dblMapHeight+i)*this._wHalfTile-cellSize, dblMapHeight*this._hHalfTile );
				}
				
				graphics.moveTo( i*this._wHalfTile-cellSize, 0 );
				if (i <= dblMapHeight)
				{
					graphics.lineTo( 0-cellSize, i*this._hHalfTile );
				}
				else
				{
					graphics.lineTo( (i - dblMapHeight)*this._wHalfTile-cellSize, dblMapHeight*this._hHalfTile );
				}
			}
			
			for (var j:int=1; j<dblMapHeight; j=j+2)
			{
				
				graphics.moveTo( 0-cellSize, j*this._hHalfTile );
				if (dblMapHeight-j >= dblMapWidth)
				{
					graphics.lineTo( dblMapWidth*this._wHalfTile-cellSize, (dblMapWidth+j)*this._hHalfTile );
				}
				else
				{
					graphics.lineTo( (dblMapHeight-j)*this._wHalfTile-cellSize, dblMapHeight*this._hHalfTile );
				}
			}
			
			for (var m:int=0; m<dblMapHeight; m=m+2)
			{
				graphics.moveTo( dblMapWidth*this._wHalfTile-cellSize, m*this._hHalfTile );
				if (dblMapWidth-dblMapHeight+m < 0)
				{
					graphics.lineTo( 0-cellSize, (dblMapWidth+m)*this._hHalfTile );
				}
				else
				{
					graphics.lineTo( (dblMapWidth-dblMapHeight+m)*this._wHalfTile-cellSize, dblMapHeight*this._hHalfTile );
				}
			}
			
			graphics.moveTo(0-cellSize, 0);
			graphics.lineStyle(0, 0, 0);
			graphics.beginFill(0x000000, 0.0);
			graphics.lineTo(_cellSize*2*_colsSize, 0);
			graphics.lineTo(_cellSize*2*_colsSize, _cellSize*_rowSize+_cellSize/2);
			graphics.lineTo(0-cellSize, _cellSize*_rowSize+_cellSize/2);
			graphics.lineTo(0-cellSize, 0);
			graphics.endFill();
		}
		
		/**
		 *更新网格 
		 */		
		public function update():void
		{
			if(_gridType==0)
			{
				drawGrid1();
			}
			else
			{
				drawGrid2();
			}
		}
		
		/**
		 *单元格子大小 
		 */		
		public function get cellSize():Number
		{
			return _cellSize;
		}
		
		/**
		 *单元格子大小 
		 */	
		public function set cellSize(value:Number):void
		{
			_cellSize = value;
		}
		
		/**
		 *总行大小 
		 */	
		public function get rowSize():Number
		{
			return _rowSize;
		}
		
		/**
		 *总行大小 
		 */	
		public function set rowSize(value:Number):void
		{
			_rowSize = value;
		}
		
		/**
		 *总列大小 
		 */	
		public function get colsSize():Number
		{
			return _colsSize;
		}
		
		/**
		 *总列大小 
		 */	
		public function set colsSize(value:Number):void
		{
			_colsSize = value;
		}
		
		/**
		 *网格颜色 
		 */	
		public function get color():uint
		{
			return _color;
		}
		
		/**
		 *网格颜色 
		 */	
		public function set color(value:uint):void
		{
			_color = value;
		}
		
		/**
		 *网格透明度
		 */	
		public function get gridAlpha():Number
		{
			return _gridAlpha;
		}
		
		/**
		 *网格透明度
		 */	
		public function set gridAlpha(value:Number):void
		{
			_gridAlpha = value;
		}
		
		override public function dispose():void
		{
			super.dispose();
			if(parent) parent.removeChild(this);
		}

		public function get gridType():int
		{
			return _gridType;
		}

		public function set gridType(value:int):void
		{
			_gridType = value;
		}

	}
}